package cn.jiyun.Controller;

import cn.jiyun.dao.Udao;
import cn.jiyun.pojo.User;
import cn.jiyun.service.Uservice;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;
@Slf4j
@EnableScheduling
@Controller
@RequestMapping("user")
public class UController {

    @Autowired
    private Uservice uservice;

    @GetMapping("findAll")
    public String findAll(Model model, @RequestParam(required = true,defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,2);
        List<User> list=uservice.findAll();
        PageInfo<User> page=new PageInfo<>(list);
        model.addAttribute("list",page);
        log.info("----------6666666-------------");

        return "ulist";
    }

    @GetMapping("hx")
    public String hx(Model model,Integer id){
        User user =uservice.hx(id);
        model.addAttribute("user",user);
        return "update";
    }

    @GetMapping("del")
    public String del(Integer id){
        uservice.del(id);
        return "redirect:findAll";
    }

    @GetMapping("tzadd")
    public String tzadd(){
        return "add";
    }

    @PostMapping("addOrupdate")
    public String addOrupdate(User user){
        if (user.getId()==null){
            uservice.add(user);
        }else {
            uservice.update(user);
        }
        return "redirect:findAll";
    }

}
